﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AddIn.EA.Framework;

namespace AddIn.EA.Architecture.CatalogService.BusinessLogic
{
    /// <summary>
    /// Classe de extensao do Framework
    /// </summary>
    public static class FrameworkExtention
    {
        private const string RETURNS_TAG = "@returns";
        
        /// <summary>
        /// Retorna o namespace do elemento sem a palavra Arquitetura
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static string GetFullNamespace(this Element element)
        {
            return element.GetNamespace().Replace("Arquitetura.", string.Empty);
        }

        /// <summary>
        /// Retorna o nome completo do elemento com o namespace
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static string GetFullName(this Element element)
        {
            return element.GetFullNamespace() + "." + element.Name;
        }

        /// <summary>
        /// Retorna o nome completo do atributo com o namespace
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static string GetFullName(this AttributeClass attribute)
        {
            return ((Element)attribute.Parent).GetFullName() + "." + attribute.Name;
        }

        /// <summary>
        /// Retorna o nome completo do metodo com o namespace utilizando reflection
        /// </summary>
        /// <param name="method"></param>
        /// <param name="fileAssembly"></param>
        /// <returns></returns>
        public static string GetFullName(this Method method, string fileAssembly)
        {
            string parentName = ((Element)method.Parent).GetFullName();

            var assemblyReflec = System.Reflection.Assembly.LoadFile(fileAssembly);
            var typeReflec = assemblyReflec.GetType(parentName);
            var methodReflec = typeReflec.GetMethod(method.Name);

            string name = methodReflec.ToString()
                .Substring(methodReflec.ToString().IndexOf(' '))
                .Replace(" ", string.Empty)
                .Replace("`1", string.Empty)
                .Replace('[', '{')
                .Replace(']', '}')
                .Replace("()", string.Empty);

            return parentName + "." + name;
        }

        /// <summary>
        /// Retorna o nome completo do metodo com o namespace
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public static string GetFullName(this Method method)
        {
            return ((Element)method.Parent).GetFullName() + "." + method.Name;
        }

        /// <summary>
        /// Retorna as notas do metodo sem comentarios do retorno
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public static string GetNotes(this Method method)
        {
            int index = method.Notes.IndexOf(RETURNS_TAG);
            if (index != -1)
                return method.Notes.Remove(index);
            else
                return method.Notes;
        }

        /// <summary>
        /// Retorna as notas do metodo sem comentarios do retorno
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public static string GetReturns(this Method method)
        {
            int index = method.Notes.IndexOf(RETURNS_TAG);
            if (index != -1)
                return method.Notes.Substring(index + RETURNS_TAG.Length + 1);
            else
                return string.Empty;
        }

    }
}
